package com.kingwood.algorithm.test;

import java.util.Scanner;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author 22025812
 * @Description: 递归相关的测试
 * @Date: 2024/3/6 14:36
 * @Modified By：
 * @Version 1.0
 */
public class DiGui {
    public static String[] cells;
    static Pattern p = Pattern.compile("(<.*?>)");

    /**
     * 数据单元的变化替换
     * https://fcqian.blog.csdn.net/article/details/134939544
     * @return
     */
    public static String dataCellReplace() {
        StringJoiner sj = new StringJoiner(",");
        for (int i=0; i<cells.length; i++) {
            if (!changeCell(i)) {
                return "-1";
            }
            sj.add(cells[i]);
        }

        return sj.toString();
    }

    public static boolean changeCell(int index) {
        Matcher m = p.matcher(cells[index]);
        while (m.find()) {
            String reference = m.group();
            if (reference.length() != 3) {
                return false;
            }

            char reference_cellNum = reference.charAt(1);
            char self_cellNum = (char)('A' + index);

            if (reference_cellNum < 'A' || reference_cellNum > 'Z' || reference_cellNum == self_cellNum) {
                return false;
            }

            int reference_index = reference_cellNum - 'A';
            if (reference_index >= cells.length) {
                return false;
            }
            if (!changeCell(reference_index)) {
                return false;
            }

            cells[index] = cells[index].replace(reference, cells[reference_index]);

            m = p.matcher(cells[index]);
        }

        return true;
    }

    /**
     * 高效货运
     * https://fcqian.blog.csdn.net/article/details/134725218
     * 此题可以使用两种解法，一种是暴力枚举，一种是完全背包（动态规划），因为此题只有两种货物使用动态规划反而复杂了
     * @param wa
     * @param wb
     * @param wt
     * @param pa
     * @param pb
     * @return
     */
    public static int truckMaxProfit(int wa, int wb, int wt, int pa, int pb) {
        int ans = 0;
        int minA = 1;
        int maxA = (wt - wb) / wa;

        for (int i = minA; i <= maxA; i++) {
            int remain = wt - i * wa;

            if (remain % wb == 0) {
                int profit = remain / wb * pb + i * pa;
                ans = Math.max(ans, profit);
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        cells = scanner.nextLine().split(",");
//        System.out.println(dataCellReplace());

        int wa = scanner.nextInt();
        int wb = scanner.nextInt();
        int wt = scanner.nextInt();
        int pa = scanner.nextInt();
        int pb = scanner.nextInt();
        System.out.println(truckMaxProfit(wa, wb, wt, pa, pb));
    }
}
